package com.lws.basic.utils.file;

import android.content.Context;
import android.content.res.AssetFileDescriptor;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.List;



public class AssetsUtils {


    /**
     * @param context
     * @param path  外来文件的地址
     * @return  将文件转化成输入流
     */
    public static InputStream readAssert(Context context, String path) {
        try {
            return context.getAssets().open(path);
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * @param context
     * @return 外来文件根目录下 文件集合
     */
    public static List<String> list(Context context)  {
        try {
            String[] strings = context.getAssets().list("");
            List<String> stringList = Arrays.asList(strings);
            return stringList;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 将外来文件拷贝至sd卡
     * @param context
     * @param assetPath  外来文件地址
     * @param filePath  文件地址
     */
    public static void copy(Context context, String assetPath, String filePath) {
        InputStream inputStream = readAssert(context,assetPath);
        FileOutputStream fileOutputStream = StreamUtils.generateOutputStream(new File(filePath));
        if (inputStream == null || fileOutputStream == null) return;
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        try {
            StreamUtils.inputOutput(bufferedInputStream,bufferedOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * @param context
     * @param path 指定目录
     * @return  判断指定目录是否为文件夹
     */
    public static boolean isDirectory(Context context, String path) {
        boolean b = false;
        try {
            b = context.getAssets().list(path).length > 0;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 将外来文件读取成映射到文件的字节缓冲区
     * @param context
     * @param assetPath  外来文件的地址
     * @return  获得映射到文件的字节缓冲区
     * @throws IOException
     */
    public static MappedByteBuffer readMappedByteBuffer(Context context, String assetPath) throws IOException {
        AssetFileDescriptor fileDescriptor = context.getAssets().openFd(assetPath);
        //获得文件输入流
        FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
        //获取文件通道
        FileChannel fileChannel = inputStream.getChannel();
        long startOffset = fileDescriptor.getStartOffset();
        long declaredLength = fileDescriptor.getDeclaredLength();
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
    }
}
